#!/usr/bin/ruby

#author CG, yes my code sux

#http://exifr.rubyforge.org/api/index.html
require 'rubygems'
require 'exifr'

if ARGV.length == 0
	puts "Usage: exifr-stalk jpeg"
	puts "Example: ./exifr-stalk 1.jpg"
	exit 0
end

photo = ARGV.shift
a = EXIFR::JPEG.new(photo)

def process_exif(a)
end
phone_make = a.exif[0].make
phone_model = a.exif[0].model

#lat long stuff from: http://www.cloudspace.com/blog/2009/02/16/decoding-gps-latitude-and-longitude-from-exif-in-ruby/

if a.exif[0].gps_latitude.nil?  or a.exif[0].gps_longitude.nil? then
	puts "No GPS data or corrupted GPS data. Exiting..."
	exit 0
else
	lat = a.exif[0].gps_latitude[0].to_f + (a.exif[0].gps_latitude[1].to_f / 60) + (a.exif[0].gps_latitude[2].to_f / 3600)
	long = a.exif[0].gps_longitude[0].to_f + (a.exif[0].gps_longitude[1].to_f / 60) + (a.exif[0].gps_longitude[2].to_f / 3600)
end

#if a.exif[0].gps_longitude == nil then
#	puts "No GPS data or corrupted gps data. Exiting..."
#	exit 0
#else
#	long = a.exif[0].gps_longitude[0].to_f + (a.exif[0].gps_longitude[1].to_f / 60) + (a.exif[0].gps_longitude[2].to_f / 3600)
#end

if a.exif[0].date_time_original == nil then
	''
else
	time_taken_original =  a.exif[0].date_time_original
end

if  a.exif[0].date_time == nil then
	''
else
	time_taken = a.exif[0].date_time
end

if a.exif[0].gps_date_stamp == nil then
	''
else
	gps_time = a.exif[0].gps_date_stamp
end

west_or_east = a.exif[0].gps_longitude_ref
north_or_south = a.exif[0].gps_latitude_ref

if west_or_east == 'W'   # (W is -, E is +)
	long = long * -1
	gps_long_ref = 'WEST'
else
	long = long
	gps_long_ref = 'EAST'
end

if north_or_south == 'S'      # (N is +, S is -)
	lat = lat * -1
	gps_lat_ref = 'SOUTH'
else
	lat = lat
	gps_lat_ref = 'NORTH'
end

#end
puts "\nPhoto Geolocation: "
puts "Latitude Reference: #{gps_lat_ref}"
puts "Latitude: #{lat}"
puts "Longitude Reference: #{gps_long_ref}"
puts "Longitude: #{long}\n\n"


puts "Camera info: "
puts "#{phone_make} #{phone_model} taken at #{time_taken}\n\n"

puts "Google Map: http://maps.google.com/maps?f=q&q=#{lat},#{long}&t=k&spn=0.5,0.5"
puts "Yahoo Map: http://maps.yahoo.com/index.php?lat=#{lat}&lon=#{long}&mvt=s&mag=5"
